package cn.net.autocode.platform.config;

import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.platform.factory.SpringBeanFactory;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

public class PlatformConfig {

    public static boolean isInit = false;

    public static final String tokenKey = "X-AUTOCODE-TOKEN";

    public static final String mobileTokenKey = "M-AUTOCODE-TOKEN";

    private static int tokenTimeout;

    private static String authType;

    public static int getTokenTimeout() {
        if(!PlatformConfig.isInit){
            PlatformConfig.init();
        }
        return Math.max(PlatformConfig.tokenTimeout, 30);
    }

    public static String getAuthType() {
        if(!PlatformConfig.isInit){
            PlatformConfig.init();
        }
        return PlatformConfig.authType;
    }


    private static void init(){
        Dao dao = SpringBeanFactory.getBeanByType(Dao.class);
        JSONArray pflist = dao.queryJSONArray("select * from autocode_platform");
        if(!pflist.isEmpty()){
            JSONObject json = pflist.getJSONObject(0);
            PlatformConfig.tokenTimeout = json.getIntValue("tokenTimeout");
            PlatformConfig.authType = json.getString("authType");
            PlatformConfig.isInit = true;
        }
    }
}
